var Notifications;

Notifications = function () {
    var settings, lastID, notifications, deletedVideosMemory, background;

    settings = new Settings();
    lastID = 0;
    notifications = [];
    deletedVideosMemory = new DeletedVideosMemory();
    background = new Background();

    this.create = function (data, timeout) {
        var notification, dataCopy;
        dataCopy = JSON.parse(JSON.stringify(data));
        dataCopy.notificationID = lastID++;
        if (!settings.getEnableNotifications()) {
            return;
        }
        notification = new Notification(data.title, {
            icon: "https://i2.ytimg.com/vi/" + data.id + "/default.jpg",
            body: data.description
        });
        notifications[dataCopy.notificationID] = notification;
        notification.onshow = function () {
            if (timeout) {
                setTimeout(function () {
                    notification.close();
                }, timeout * 1000);
            }
        };
        notification.onclose = function () {
            notifications[dataCopy.notificationID] = null;
        };
        notification.onerror = function () {
            notifications[dataCopy.notificationID] = null;
        };
        notification.onclick = function (e) {
            var activeTab;
            if (settings.getRemoveWatched()) {
                deletedVideosMemory.onVideoDeleted(data.id);
                background.reload();
            }
            activeTab = background.getTabID();
            if (activeTab) {
                chrome.tabs.update(activeTab, {
                    url: "https://www.youtube.com/watch?v=" + data.id,
                    selected: false
                });
                notification.close();
                return;
            }
            chrome.tabs.create({
                url: "https://www.youtube.com/watch?v=" + data.id,
                selected: true
            }, function (tab) {
                background.setTabID(tab.id);
            });
            notification.close();
        };
    };

    Notifications.getNotification = function (id) {
        return notifications[id];
    };
};
